package cn.dengta.webapp.ai.model;

import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson2.annotation.JSONField;
import lombok.Getter;
import lombok.Setter;

/**
 * 参考：https://www.minimaxi.com/document/guides/chat-model/pro/api?id=6569c85948bc7b684b30377e
 * 限制返回格式功能
 */
@Getter
@Setter
public class Glyph {

	private String type;

	public Glyph() {
	}

	private Glyph(String type) {
		this.type = type;
	}

	public static RawGlyph ofRaw(String rawGlyph) {
		return new RawGlyph("raw", rawGlyph);
	}

	public static JsonGlyph ofJson() {
		return new JsonGlyph("json_value");
	}

	/**
	 * * eg：
	 * "glyph": {
	 * "type": "raw",
	 * "raw_glyph": "这句话的翻译是：{{gen 'content'}}"
	 * }
	 * * <p>
	 * * 问题：请用英语帮我翻译下面这句话：我是谁
	 * * 返回结果："这句话的翻译是：\"Who am I?\""
	 */
	@Setter
	@Getter
	public static class RawGlyph extends Glyph {

		@JSONField(name = "raw_glyph")

		private String rawGlyph;

		public RawGlyph() {
		}

		private RawGlyph(String type, String rawGlyph) {
			super(type);
			this.rawGlyph = rawGlyph;
		}

	}

	/**
	 * * eg：
	 * * glyph：{
	 * * "name": {
	 * * "type": "string"
	 * * },
	 * * "age": {
	 * * "type": "number"
	 * * },
	 * * "is_student": {
	 * * "type": "boolean"
	 * * },
	 * * "reply": {
	 * * "type": "string"
	 * * },
	 * * "courses": {
	 * * "type": "object",
	 * * "properties": {
	 * * "name": {
	 * * "type": "string"
	 * * },
	 * * "score": {
	 * * "type": "number"
	 * * }
	 * * }
	 * * }
	 * * }
	 * * <p>
	 * * 问题：你在哪个学校读书，今年几岁，几年级，成绩怎么样
	 * * 返回结果：
	 * * {
	 * * "name": "韩多多",
	 * * "reply": "我在江城八中读高二，今年17岁，读高二，成绩属于中上游水平，我对自己还是很有自信的。",
	 * * "is_student": true,
	 * * "courses":   {
	 * * "name": "语文",
	 * * "score": 90
	 * * },
	 * * "age": 17
	 * * }
	 */
	@Setter
	@Getter
	public static class JsonGlyph extends Glyph {

		public JsonGlyph() {
		}

		@JSONField(name = "json_properties")
		private Map<String, Base> jsonProperties;

		@JSONField(serialize = false, deserialize = false)
		protected boolean keySort;

		public void addStr(String key) {
			jsonProperties.put(key, Base.ofStr());
		}

		public void addNum(String key) {
			jsonProperties.put(key, Base.ofNum());
		}

		public void addBool(String key) {
			jsonProperties.put(key, Base.ofBool());
		}

		public Obj addObj(String key) {
			Obj obj = Obj.ofObj();
			jsonProperties.put(key, obj);
			return obj;
		}

		public void addArr(String key, Base type) {
			jsonProperties.put(key, Arr.ofArr(type));
		}

		private JsonGlyph(String type) {
			super(type);
			this.jsonProperties = new HashMap<>();
		}

		@Getter
		@Setter
		public static class Base {

			protected String type;

			public static Base ofStr() {
				Base base = new Base();
				base.type = "string";
				return base;
			}

			public static Base ofNum() {
				Base base = new Base();
				base.type = "number";
				return base;
			}

			public static Base ofBool() {
				Base base = new Base();
				base.type = "boolean";
				return base;
			}

		}

		@Getter
		@Setter
		public static class Obj extends Base {

			private Map<String, Base> properties;

			private static Obj ofObj() {
				Obj obj = new Obj();
				obj.type = "object";
				obj.properties = new HashMap<>();
				return obj;
			}

			public void addStr(String key) {
				properties.put(key, Base.ofStr());
			}

			public void addNum(String key) {
				properties.put(key, Base.ofNum());
			}

			public void addBool(String key) {
				properties.put(key, Base.ofBool());
			}

			public Obj addObj(String key) {
				Obj obj = Obj.ofObj();
				properties.put(key, obj);
				return obj;
			}

			public void addArr(String key, Base type) {
				properties.put(key, Arr.ofArr(type));
			}

		}

		@Getter
		@Setter
		public static class Arr extends Base {

			private Base items;

			public static Arr ofArr(Base base) {
				Arr arr = new Arr();
				arr.type = "array";
				arr.items = base;
				return arr;
			}

		}

	}

}
